package tools;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Permet de lire les différents formats de clé utilisés.
 * @author Guillaume Poças
 */
public class KeyParser {
	
	/**
	 * Constructeur vide.
	 */
	public KeyParser(){	}
	
	/**
	 * Lire une fichier contenant une clé.
	 * @param algo
	 * @param filePath
	 * @return la clé
	 */
	public static String parseKey(String algo, String filePath) {
		BufferedReader br = null;
        FileInputStream file = null;
        String line, key = "";
        try {
			file = new FileInputStream(filePath);
			br = new BufferedReader(new InputStreamReader(file));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
        
        if(algo.equalsIgnoreCase("a51")){
        	try {
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- KEY BEGIN ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				//key
				key += line;
				key+= ";";
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- KEY END ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- COUNTER BEGIN ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				//counter
				key += line;
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- COUNTER END ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
        	
        	try {
    			file.close();
    			br.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        	return key;
        	
        } else if(algo.equalsIgnoreCase("lfsr")){
        	
        	try {
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- LENGTH BEGIN ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				//length
				key += line;
				key+= ";";
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- LENGTH END ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- REGISTER BEGIN ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				//register
				key += line+";";
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- REGISTER END ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- FEEDBACK BEGIN ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
				
				line = br.readLine();
				//retroaction
				key += line;
				
				line = br.readLine();
				if(!line.equalsIgnoreCase("---------- FEEDBACK END ----------")){
					System.out.println("Mauvais format de la cle");
					return null;
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
        	
        	try {
        		file.close();
        		br.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        	return key;
        }
        else if(algo.equalsIgnoreCase("ZeroGenerator")) {
        	
        	try {
        		file.close();
        		br.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        	return "";
        }
        else {
        	try {
        		file.close();
        		br.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
        	return null;
        }
			
	}
	
}
